﻿package com.eshore.player;

import java.nio.ByteBuffer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import android.widget.RelativeLayout;


public class ImagePlayerView extends RelativeLayout {
	// private Context context = null;
	protected int screenWidth = 0;
	protected int screenHeight = 0;
	protected int orientation = 0; // 0:Portrait; 1:Landscape
	protected DrawCanvasView canvasview = null;

	protected int cGetImageWidth = 352;
	protected int cGetImageHeight = 288;
	protected int cGetImageFps = 30;
	protected Bitmap cImage = null;

	protected int screenMode = 0;
	public static int MODE_FULLSCREEN = 0;
	public static int MODE_STAND = 1;

	// public static int MODE_BIGGER = 2;

	protected static native int ffplayConnect(String filename);

	protected static native int ffplayPlay();

	protected static native int ffplayStop();
	
	protected static native int ffplayPause();
	
	protected static native int ffplayResume();

	protected static native void ffplayCreate(int width, int height);

	protected static native int ffplayIsReady();

	protected static native int ffplayImageWidth();

	protected static native int ffplayImageHeight();

	protected static native int[] ffplayGetImage();

	protected static native int ffplayPlayAndPushImage(ImagePlayerView object); // 可以调用ImagePlayerView中的drawImage(int[]
																				// sPixels,
																				// int
																				// width,
																				// int
																				// height)方法

	public static boolean initLib() {
		try {
			 System.loadLibrary("sdl");
			 System.loadLibrary("avutil");
			 System.loadLibrary("avcodec");
			 System.loadLibrary("avformat");
			 System.loadLibrary("swscale");
			 System.loadLibrary("ffplay-android");
			return true;
		} catch (UnsatisfiedLinkError e) {
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	public int getcGetImageWidth() {
		return cGetImageWidth;
	}

	public void setcGetImageWidth(int cGetImageWidth) {
		this.cGetImageWidth = cGetImageWidth;
	}

	public int getcGetImageHeight() {
		return cGetImageHeight;
	}

	public void setcGetImageHeight(int cGetImageHeight) {
		this.cGetImageHeight = cGetImageHeight;
	}

	public int getcGetImageFps() {
		return cGetImageFps;
	}

	public void setcGetImageFps(int cGetImageFps) {
		this.cGetImageFps = cGetImageFps;
	}

	public int getScreenWidth() {
		return screenWidth;
	}

	public void setScreenWidth(int screenWidth) {
		this.screenWidth = screenWidth;
	}

	public int getScreenHeight() {
		return screenHeight;
	}

	public void setScreenHeight(int screenHeight) {
		this.screenHeight = screenHeight;
	}

	public int getOrientation() {
		return orientation;
	}

	public void setOrientation(int orientation) {
		this.orientation = orientation;
	}

	public DrawCanvasView getCanvasview() {
		return canvasview;
	}

	public Bitmap getcImage() {
		if (cImage == null) {
			this.createBitmap();
		}
		return cImage;
	}

	public ImagePlayerView(Context context, int screenMode, int screenWidth,
			int screenHeight) {
		super(context);
		// this.context = context;
		this.screenMode = screenMode;
		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
		this.canvasview = new DrawCanvasView(context);
		this.addView(this.canvasview);
		// this.init();
	}

	public void setScreenMode(int screenMode) {
		this.screenMode = screenMode;
	}

	protected void init() {

	}

	public void start() {

	}

	public void stop() {

	}

	public void drawImage(ByteBuffer buffer/* int[] sPixels */, int width,
			int height) {

	}

	protected void createBitmap() {
		cImage = Bitmap.createBitmap(cGetImageWidth, cGetImageHeight,
				Bitmap.Config.ARGB_8888);
	}

	protected int getImageWidth(int returnMode) {
		int width = 0;
		int height = 0;
		int getImageWidth = this.cGetImageWidth;
		int getImageHeight = this.cGetImageHeight;
		int tmpScreenMode = this.screenMode;
		int tmpScreenWidth = this.screenWidth;
		int tmpScreenHeight = this.screenHeight;
		// if(this.orientation == 1) {
		// tmpScreenWidth = this.screenHeight;
		// tmpScreenHeight = this.screenWidth;
		// }
		Log.d("ImagePlayer", "real image:" + getImageWidth + "x"
				+ getImageHeight);
		if (getImageWidth > getImageHeight) {
			switch (tmpScreenMode) {
			case 0: // ImagePlayerView.MODE_FULLSCREEN
				if (tmpScreenWidth > tmpScreenHeight) {
					height = tmpScreenHeight;
					width = getImageWidth * height / getImageHeight;
					if (width > tmpScreenWidth) {
						width = tmpScreenWidth;
						height = getImageHeight * width / getImageWidth;
					}
				} else {
					width = tmpScreenWidth;
					height = getImageHeight * width / getImageWidth;
					if (height > tmpScreenHeight) {
						height = tmpScreenHeight;
						width = getImageWidth * height / getImageHeight;
					}
				}
				break;
			case 1: // ImagePlayerView.MODE_STAND
				width = getImageWidth;
				if (width > tmpScreenWidth) {
					height = tmpScreenHeight;
					width = getImageWidth * height / getImageHeight;
				} else {
					if (getImageHeight > tmpScreenHeight) {
						height = tmpScreenHeight;
						width = getImageWidth * height / getImageHeight;
					}
				}
				break;
			// case 2: //ImagePlayerView.MODE_BIGGER
			// break;
			}
		} else {
			switch (tmpScreenMode) {
			case 0: // ImagePlayerView.MODE_FULLSCREEN
				if (tmpScreenWidth > tmpScreenHeight) {
					height = tmpScreenHeight;
					width = getImageWidth * height / getImageHeight;
					if (width > tmpScreenWidth) {
						width = tmpScreenWidth;
						height = getImageHeight * width / getImageWidth;
					}
				} else {
					width = tmpScreenWidth;
					height = getImageHeight * width / getImageWidth;
					if (height > tmpScreenHeight) {
						height = tmpScreenHeight;
						width = getImageWidth * height / getImageHeight;
					}
				}
				break;
			case 1: // ImagePlayerView.MODE_STAND
				width = getImageWidth;
				if (width > tmpScreenWidth) {
					height = tmpScreenHeight;
					width = getImageWidth * height / getImageHeight;
				} else {
					if (getImageHeight > tmpScreenHeight) {
						height = tmpScreenHeight;
						width = getImageWidth * height / getImageHeight;
					}
				}
				break;
			// case 2: //ImagePlayerView.MODE_BIGGER
			// break;
			}
		}
		if (returnMode == 0) { // width
			return width;
		} else {
			return height;
		}

	}

	protected Rect getImageRect() {
		int newwidth = 352;
		int newheight = 288;
		int screenWidth = this.getScreenWidth();
		int screenHeight = this.getScreenHeight();
		newwidth = this.getImageWidth(0);
		newheight = this.getImageWidth(1);
		Log.d("ImagePlayer", "image:" + newwidth + "x"
				+ newheight);
		int newleft = (screenWidth - newwidth) / 2;
		int newtop = (screenHeight - newheight) / 2;
		Log.d("ImagePlayer", "screen:" + screenWidth
				+ "x" + screenHeight);
		Log.d("ImagePlayer", "location: top=" + newtop
				+ "; left=" + newleft);
		return new Rect(newleft, newtop, newwidth + newleft, newheight + newtop);
	}
	
	public void checkImage() {
		Bitmap cImage = this.getcImage();
		Rect rect = this.getImageRect();
		this.getCanvasview().drawBitmap(cImage, rect);
	}
	
}
